package com.bizdk.fastfx.controller;

import com.bizdk.fastfx.domain.entity.Doctor;
import com.bizdk.fastfx.Context;
import com.bizdk.fastfx.fxsupport.FxmlView;
import com.bizdk.fastfx.fxsupport.JavaFxController;
import com.bizdk.fastfx.fxsupport.StageManager;
import com.bizdk.fastfx.service.DoctorService;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.ResourceBundle;

@Controller
@FxmlView("/view/LoginStage.fxml")
public class LoginStageController extends JavaFxController {
    @FXML
    private Button bt_login;
    @FXML
    private TextField tf_username;
    @FXML
    private PasswordField pf_passwd;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private MainStageController mainStageController;

    public void initialize(URL location, ResourceBundle resources) {
        tf_username.setText("admin");
        pf_passwd.setText("admin");
    }

    public void hanlderLogin(ActionEvent event){
        event.consume();
        String username = tf_username.getText();
        String passwd = pf_passwd.getText();
        Doctor doctor = doctorService.findOne(username, passwd);
        if (null != doctor) {
            showMainVirew(false);
            return;
        } else if ("admin".equals(username) && "admin".equals(passwd) ) {
            showMainVirew(true);
            return;
        } else {
            showAlert("用户名或密码不正确", Alert.AlertType.ERROR);
        }
    }

    public void showMainVirew(boolean isAdmin){
        Context.isAdmin = isAdmin;
        mainStageController.show();
        StageManager.close(getStage());
    }

    private void showAlert(String message, Alert.AlertType type) {
        Alert alert = new Alert(type);
        alert.setTitle("提示");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

}